<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Pagination {

	protected $_total = 0;
	protected $_limit = 0;
	protected $_current_page = 0;
	protected $_url_prefix = '';
	protected $_class = 'paging clearfix';

	public function __construct(){} 

	public function reset(){
		$this->_total = 0;
		$this->_limit = 0;
		$this->_current_page = 0;
		$this->_url_prefix = '';
		$this->_class = 'paging clearfix';
	}

	public function setTotal($total){
		$this->_total = $total;
	}

	public function setLimit($limit){
		$this->_limit = $limit;
	}

	public function setCurrentPage($page){
		$this->_current_page = $page;
	}

	public function setUrlPrefix($url){
		$this->_url_prefix = $url;
	}
	
	public function generateHTML(){
		$total_page = ceil($this->_total/$this->_limit);
		if($this->_current_page > $total_page) $this->_current_page = $total_page;
		if($this->_current_page < 1) $this->_current_page = 1;

		if($this->_current_page >= $total_page - 4){
			$start = max(1,$total_page - 8);
		}else{
			$start = max(1,$this->_current_page - 4);
		}
		if($this->_current_page <= 5){
			$end = min($total_page,9);
		}else{
			$end = min($total_page,$this->_current_page + 4);
		}


		$html = '<div class="'.$this->_class.'">';

		if($this->_current_page > 1){
			$html .= '<a href="'.$this->_url_prefix.'page='.($this->_current_page - 1).'"><</a>';
		}
		if($start > 1) {
			$html .= '<a href="'.$this->_url_prefix.'page=1">1</a><em>...</em>';
		}
		for($i=$start;$i<=$end;$i++){
			if($i == $this->_current_page){
				$html .= '<span>'.$i.'</span>';
			}else{
				$html .= '<a href="'.$this->_url_prefix.'page='.$i.'">'.$i.'</a>';
			}
		}
		if($end < $total_page) {
			$html .= '<em>...</em><a href="'.$this->_url_prefix.'page='.$total_page.'">'.$total_page.'</a>';
		}
		if($this->_current_page < $total_page){
			$html .= '<a href="'.$this->_url_prefix.'page='.($this->_current_page + 1).'">></a>';
		}

		$html .= '</div>';

		return $html;
	}
}

/* End of file pagination.php */
/* Location: ./application/libraries/pagination.php */